package com.semidata.trp.decision.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;

import com.semidata.rtd.core.http.server.DefaultDispatcher;
import com.semidata.rtd.core.http.server.HttpDispatchHandler;
import com.semidata.rtd.core.http.server.UrlMatcher;
import com.semidata.trp.decision.config.Config;

public class DecisionHttpDispatcher extends DefaultDispatcher {

    public DecisionHttpDispatcher() {
        super();

        Config config = Config.getConfig();
       
        String urlDecision = config.getUrlDecision();
        String urlBusiness = config.getUrlBusiness();
        String urlBudget = config.getUrlBudget();
        String urlBusinessJson = config.getUrlBusinessJson();
        String urlBudgetJson = config.getUrlBudgetJson();
        String urlReport = config.getUrlReport();

        if (!urlBusiness.endsWith("/")) {
            urlBusiness += "/";
        }
        
        if (!urlBudget.endsWith("/")) {
            urlBudget += "/";
        }
        
        if (!urlBusinessJson.endsWith("/")) {
            urlBusinessJson += "/";
        }
        
        if (!urlBudgetJson.endsWith("/")) {
            urlBudgetJson += "/";
        }

        if (!urlDecision.endsWith("/")) {
            urlDecision += "/";
        }
        
        if (!urlReport.endsWith("/")) {
            urlReport += "/";
        }

        addUrlMatcher(new UrlMatcher(urlDecision) {

            @Override
            protected HttpDispatchHandler createHandler(ChannelHandlerContext ctx, HttpRequest request) {
                return new DecisionHttpHandler(ctx, request);
            }
        });
        
        addUrlMatcher(new UrlMatcher(urlBusiness) {

            @Override
            protected HttpDispatchHandler createHandler(ChannelHandlerContext ctx, HttpRequest request) {
                return new BusinessHttpHandler(ctx, request);
            }
        });
        
        addUrlMatcher(new UrlMatcher(urlBudget) {

            @Override
            protected HttpDispatchHandler createHandler(ChannelHandlerContext ctx, HttpRequest request) {
                return new BudgetHttpHandler(ctx, request);
            }
        });
        
        addUrlMatcher(new UrlMatcher(urlBusinessJson) {

            @Override
            protected HttpDispatchHandler createHandler(ChannelHandlerContext ctx, HttpRequest request) {
                return new BusinessJsonHttpHandler(ctx, request);
            }
        });
        
        addUrlMatcher(new UrlMatcher(urlBudgetJson) {

            @Override
            protected HttpDispatchHandler createHandler(ChannelHandlerContext ctx, HttpRequest request) {
                return new BudgetJsonHttpHandler(ctx, request);
            }
        });
        
        addUrlMatcher(new UrlMatcher(urlReport){

			@Override
			protected HttpDispatchHandler createHandler(
					ChannelHandlerContext ctx, HttpRequest request) {
				return new ReportHttpHandler(ctx, request);
			}
        	
        });
    }

}
